package com.example.zzh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zzh.entity.ZzhDepot;
import com.example.zzh.entity.ZzhOrganization;
import com.example.zzh.entity.ZzhRole;
import com.example.zzh.entity.ZzhStaff;
import com.example.zzh.service.ZzhDepotService;
import com.example.zzh.service.ZzhRoleService;
import com.example.zzh.service.ZzhStaffService;
import com.example.zzh.utils.Result;
import com.example.zzh.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 仓库表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-08-18
 */
@RestController
@RequestMapping("/zzh-depot")
public class ZzhDepotController {

    @Autowired
    private ZzhStaffService zzhStaffService;

    @Autowired
    private ZzhDepotService zzhDepotService;

    @GetMapping("/option")
    @ApiOperation(value = "查询下拉角色和部门列表", notes = "查询下拉角色和部门列表")
    public Result option(){
        List<ZzhStaff> zzhStaffs=zzhStaffService.list();
        Map<Long,String> hashMapStaff=new HashMap<Long, String>();
        for (ZzhStaff i : zzhStaffs) {
            hashMapStaff.put(i.getId(),i.getUsername());
        }

        Map<String,Object> data=new HashMap<>();
        data.put("hashMapStaff",hashMapStaff);
        return ResultGenerator.genSuccessResult(data);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询所有仓库分页", notes = "分页")
    public Result list(@RequestBody Map<String,Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        Page<ZzhDepot> page=new Page<>();
        page.setCurrent((int) params.get("page"));
        page.setSize((int) params.get("limit"));
        LambdaQueryWrapper<ZzhDepot> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage r=null;
        if(!StringUtils.isEmpty(params.get("name"))){
            lambdaQueryWrapper.like(ZzhDepot::getName,params.get("name"));
        }
        if(!StringUtils.isEmpty(params.get("remark"))){
            lambdaQueryWrapper.eq(ZzhDepot::getRemark,params.get("remark"));
        }

        lambdaQueryWrapper.in(ZzhDepot::getStatus,1,2);
        r=zzhDepotService.pageNames(page,lambdaQueryWrapper);
        return ResultGenerator.genSuccessResult(r.getRecords(),r.getTotal());
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "修改仓库状态", notes = "修改仓库状态")
    public Result updateStatus(@RequestParam Long id){
        ZzhDepot zzhDepot=zzhDepotService.getById(id);
        if(!Objects.isNull(zzhDepot)){
            if(zzhDepot.getStatus()==1){
                zzhDepot.setStatus(2);
            }else if(zzhDepot.getStatus()==2){
                zzhDepot.setStatus(1);
            }
            if(zzhDepotService.updateById(zzhDepot)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增仓库", notes = "新增仓库信息")
    public Result add(@RequestBody ZzhDepot zzhDepot){
        if(zzhDepotService.save(zzhDepot)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改仓库", notes = "修改仓库信息")
    public Result mod(@RequestBody ZzhDepot zzhDepot){
        if(zzhDepotService.updateById(zzhDepot)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"修改不成功");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除仓库", notes = "删除仓库信息")
    public Result delete(@RequestParam Long id){
        ZzhDepot zzhDepot=zzhDepotService.getById(id);
        if(!Objects.isNull(zzhDepot)){
            zzhDepot.setStatus(0);
            if(zzhDepotService.updateById(zzhDepot)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }

}
